Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the performance of the FusionCacheProvider #249

Merged
merged 8 commits into from
Jun 5, 2024

Conversation

0xced
Copy link
Contributor

@0xced 0xced commented May 28, 2024

Storing the caches in a dictionary upfront allows to retrieve the cache in O(1) instead of O(n).

Using Lazy<IFusionCache> allows to preserve the semantic of throwing when accessing the (incorrectly configured) caches.

Also, the DependencyInjectionTests were very slightly improved:

  • Using GetRequiredService instead of GetService plus the null-forgiving operator because an InvalidOperationException is always better than a NullReferenceException, even in unit tests.
  • A new assertion was added to ensure that the default IFusionCache instance is always the same no matter how it's retrieved, either through IFusionCacheProvider or directly by requesting the IFusionCache.

0xced added 3 commits May 28, 2024 14:14
An `InvalidOperationException` is always better than a `NullReferenceException`, even in unit tests.
No matter how it's retrieved, either through IFusionCacheProvider or directly by requesting the IFusionCache.
By storing the caches in a dictionary so that retrieving the cache can be performed in O(1) instead of O(n).
@jodydonetti
Copy link
Collaborator

Hi @0xced I updated this branch based on the new main, and pushed some changes which build upon your work and can make it even better.

Basically instead of storing an IEnumerable to iterate on at every GetCache() call, I just stored either a single LazyNamedCache in case there was only one, or null in case they were more than 1: in this way no need to an iteration per-call, less cpu usage and less allocations.

Finally I've made the tests a little better around these scenarios.

Thoughts?

@jodydonetti jodydonetti added this to the v1.3.0 milestone Jun 4, 2024
@jodydonetti jodydonetti self-assigned this Jun 4, 2024
@jodydonetti jodydonetti added the enhancement New feature or request label Jun 4, 2024
Copy link
Collaborator

@jodydonetti jodydonetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest changes it LGTM: if you agree I can proceed and merge this.

@jodydonetti jodydonetti changed the title Improve the performance of the FusionCacheProvider Fusion cache provider Improve the performance of the FusionCacheProvider Jun 5, 2024
@0xced
Copy link
Contributor Author

0xced commented Jun 5, 2024

Looks good to me too!

@jodydonetti jodydonetti merged commit 42c7ce6 into ZiggyCreatures:main Jun 5, 2024
1 check failed
@0xced 0xced deleted the FusionCacheProvider branch June 5, 2024 20:33
@jodydonetti
Copy link
Collaborator

Hi @0xced , v1.3.0 is out 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants